package com.eagle.epay.controller;


import com.eagle.epay.common.ResultVO;
import com.eagle.epay.constant.EPayConstant;
import com.eagle.epay.entity.User;
import com.eagle.epay.form.CartAddForm;
import com.eagle.epay.form.CartUpdateForm;
import com.eagle.epay.service.IShoppingCartService;
import com.eagle.epay.util.ResultVOUtil;
import com.eagle.epay.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author eagle
 * @since 2019-12-21
 */
@RestController
@RequestMapping("/shoppingcart")
public class ShoppingCartController {

	@Autowired
	IShoppingCartService shoppingCartService;

	@PostMapping("/add")
	public ResultVO<CartVo> add(@Valid @RequestBody CartAddForm cartAddForm,
								HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		CartVo s = shoppingCartService.addToCart(String.valueOf(curUser.getId()),cartAddForm);
		return ResultVOUtil.success(s);
	}

	@PostMapping("/update")
	public ResultVO<CartVo> update(@Valid @RequestBody CartUpdateForm cartUpdateForm, HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		CartVo s = shoppingCartService.updateCart(String.valueOf(curUser.getId()),cartUpdateForm);
		return ResultVOUtil.success(s);
	}

	@PostMapping("/delete")
	public ResultVO<CartVo> delete(@Valid @RequestBody CartUpdateForm cartUpdateForm, HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		CartVo s = shoppingCartService.deleteCart(String.valueOf(curUser.getId()),cartUpdateForm);
		return ResultVOUtil.success(s);
	}

	@GetMapping("/selectAll")
	public ResultVO<CartVo> selectAll(HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		CartVo s = shoppingCartService.selectAll(String.valueOf(curUser.getId()));
		return ResultVOUtil.success(s);
	}

//	http://localhost:8080/shoppingcart/unSelectAll?uid=cart_eagle
	@GetMapping("/unSelectAll")
	public ResultVO<CartVo> unSelectAll(HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		CartVo s = shoppingCartService.unSelectAll(String.valueOf(curUser.getId()));
		return ResultVOUtil.success(s);
	}

	@GetMapping("/totalQuantity")
	public ResultVO<CartVo> totalQuantity(HttpSession session) throws Exception{
		User curUser =(User) session.getAttribute(EPayConstant.CURUSER);
		Integer s = shoppingCartService.totalQuantity(String.valueOf(curUser.getId()));
		return ResultVOUtil.success(s);
	}

}

